<script type="text/javascript" >
    stPublishToolbox.sortColumn();
    stPublishToolbox.shortAction();
       jQuery(document).ready(function(){
           jQuery('.tooltip').tooltip({
                track: true,
                delay: 0,
                showURL: false,
                showBody: " - ",
                fade: 250
            });
          jQuery("#link a[rel^='prettyPhoto']").prettyPhoto({theme:'facebook', keyboard_shortcuts: false});
           
        FB.init({
            appId:'<?php echo $config['facebook_app_id']; ?>', cookie:true,
            status:true, xfbml:true
        });
        <?php if($logoff == 'true'){?>
        <?php echo 'logoff();'; } else {?>
        FB.Event.subscribe('auth.sessionChange', function(response) {
            if (response.session) {
                var access_token = response.session.access_token;
                var session_key = response.session.session_key;
                var uid = response.session.uid;
                stPublishToolbox.loadPage("list_wall&access_token="+access_token+"&session_key="+session_key+"&uid="+uid+"&login=true", '', '', function(){
                    window.location = '<?php echo get_bloginfo('url'); ?>/wp-admin/admin.php?page=stPublishToolbox';
                });
            }

        });
<?php }?>
    });
    function logoff(){
        FB.getLoginStatus(function(response) {
            if (response.session) {
                FB.logout(function(response) {
                    stPublishToolbox.loadPage("list_wall&login=true", '', '', function(){
                        window.location = '<?php echo get_bloginfo('url'); ?>/wp-admin/admin.php?page=stPublishToolbox';
                    });
                });
            } else {
                stPublishToolbox.loadPage("list_wall&login=true", '', '', function(){
                    window.location = '<?php echo get_bloginfo('url'); ?>/wp-admin/admin.php?page=stPublishToolbox';
                });
            }
        });


    }
</script>
<div id="fb-root"></div>
<div class="w100p bgl">
    <div class="w100p bgr">
        <div class="inner_container">
            Only show scheduled content <input type="checkbox"  onclick="if(this.checked){ display = 1;}else {display = 0}stPublishToolbox.loadPage('dashboard&display='+display);" <?php if($publish_display == 1){echo 'checked';} ?> />
            <p></p>
           
            <table class="widefat" cellspacing="0" >
                <thead>
                    <tr>
                        <?php print_column_headers('stFacebookToolbox_stFacebookToolbox'); ?>
                    </tr>
                </thead>

                <tbody>
                <?php
                global $wpdb;
                if (count($results) > 0) {
                    $tr_class = "";
                    $upload_data = wp_upload_dir();
                    
                    foreach ($results as $result) {
                        if( empty($tr_class) || $tr_class == "class='bg_gray2'"){
                            $tr_class = "class='bg_gray1'";
                        }else{
                            $tr_class = "class='bg_gray2'";
                        }
                            if($result->publish_type == 'facebook' && count(json_decode($result->page)) > 0){
                            $array_page = array();
                            $array_page_name = array();
                            foreach(json_decode($result->page) as $row)
                            {
                                    //$array_page[] = 'http://www.facebook.com/profile.php?id='.$row;
                                    $array_page_name[] = $wpdb->get_var("SELECT pagename FROM st_likepluginfbpagelist WHERE pageid=".$row);
                            }
                            if($result->id_post != ''){
                            foreach(json_decode($result->id_post) as $row){
                                $array_page[] = $row;
                            }
                            }
                        if(is_array($array_page)){
                        $page = implode(';',$array_page);
                        $page = '"'.$page.'"';
                        }
                        $page_name = implode(', ',$array_page_name);
                        $title = 'Published to Facebook Page "'.$page_name.'"';
                        $image ="<img src='{$config['imagesUrl']}facebook.gif' onclick='stPublishToolbox.popup({$page});return false;' style='cursor:pointer;' />";
                        if($wpdb->get_var("SELECT COUNT(*) FROM st_publishing WHERE last_update = '{$result->last_update}' AND user_id = $result->user_id AND (publish_type = 'twitter' OR publish_type = 'flickr')") > 0){
                            $publish_type = $wpdb->get_var("SELECT publish_type FROM st_publishing WHERE last_update = '{$result->last_update}' AND user_id = $result->user_id AND (publish_type = 'twitter' OR publish_type = 'flickr')");
                            $title .= ', '.ucfirst($publish_type);
                            $id_post = $wpdb->get_var("SELECT id_post FROM st_publishing WHERE last_update = '{$result->last_update}' AND user_id = $result->user_id AND (publish_type = 'twitter' OR publish_type = 'flickr')");
                            if($id_post != ''){
                                $array_flickr = array();
                            foreach(json_decode($id_post) as $id){
                                $array_flickr[] = $id;
                            }
                            $page_flickr = implode(';',$array_flickr);
                            $page_flickr = '"'.$page_flickr.'"';
                            $image .= '&nbsp'."<img src='{$config['imagesUrl']}{$publish_type}.png' onclick='stPublishToolbox.popup({$page_flickr});return false;' style='cursor:pointer;' />";
                            }
                            else {
                                $image .= '&nbsp'."<img src='{$config['imagesUrl']}{$publish_type}.png'  style='cursor:pointer;' />";
                            }
                           
                            
                        }
                        }
                        else
                        {
                            
                            if($result->id_post != ''){
                                $array_flickr = array();
                            foreach(json_decode($result->id_post) as $id){
                                $array_flickr[] = $id;
                            }
                                 $page_flickr = implode(';',$array_flickr);
                                 $page_flickr = '"'.$page_flickr.'"';
                                 $image = "<img src='{$config['imagesUrl']}{$result->publish_type}.png' onclick='stPublishToolbox.popup({$page_flickr});return false;' style='cursor:pointer;' />";
                            }
                            else {
                                 $image = "<img src='{$config['imagesUrl']}{$result->publish_type}.png' />";
                            }
                            $title = 'Published to '.ucfirst($result->publish_type);
                           
                            if($wpdb->get_var("SELECT COUNT(*) FROM st_publishing WHERE last_update = '{$result->last_update}' AND user_id = $result->user_id AND publish_type = 'facebook'") > 0){
                                $page = $wpdb->get_var("SELECT page FROM st_publishing WHERE last_update = '{$result->last_update}' AND user_id = $result->user_id AND publish_type = 'facebook'");
                                if(count(json_decode($page)) >0){
                                $array_page = array();
                                $array_page_name = array();
                                foreach(json_decode() as $fa)
                                {
                                        //$array_page[] = 'http://www.facebook.com/profile.php?id='.$fa;
                                        $array_page_name[] = $wpdb->get_var("SELECT pagename FROM st_likepluginfbpagelist WHERE pageid=".$fa);
                                }

                             if($result->id_post != ''){
                            foreach(json_decode($result->id_post) as $row){
                                $array_page[] = $row;
                            }
                            }
                        if(is_array($array_page)){
                        $page = implode(';',$array_page);
                        $page = '"'.$page.'"';
                        }
                            $page_name = implode(', ',$array_page_name);
                            $title .= ', Facebook Page "'.$page_name.'"';
                            $image .= "&nbsp;<img src='{$config['imagesUrl']}facebook.gif' onclick='stPublishToolbox.popup({$page});return false;' style='cursor:pointer;' />";
                            }
                            
                        }
                        
                        }
                        echo "<tr id='$result->id' $tr_class >"
                        . "<td title='{$result->content}' class='tooltip' style='cursor:pointer;'>" . $result->name;
                        if($result->publish_type == 'facebook'){
                        echo "<div class=\"row-actions\">"
                        . "<a href='javascript:void(0);'  onclick='stPublishToolbox.popup({$page});return false;' style='color:#21759B;text-decoration:none;'>View</span>"
                        . "</div>";
                        }
                        echo "</td>"
                        . "<td>" . str_replace('swf','flash',$result->type) . "</td>"
                        . "<td>" ;
                        echo ($result->published == 1)?'pending':'published' ;
                        echo "</td>"
                        . "<td>" . $result->time . "</td>"
                        . "<td>" . $result->last_update . "</td>"
                        . "<td title='{$title}' class='tooltip' style='cursor:pointer;' align='center'>{$image}</td>"
                        . "<td>" . $result->username . "</td>"
                        . "</tr>"
                        ;
                    }
                }
                ?>
                </tbody>
            </table>
            <div class="page_d">
                <?php echo $pagination?>
                <div class="cl"></div>
            </div>

        </div>
    </div>
</div>